<%--
	author:daniel.yan
	time:2012/4/22 12:09:13
--%>
<%@page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib uri="TowkerTagLib" prefix="rbac"%>
<%@include file="/commons/include/html_doctype.html"%>
<html>
<head>
<title>修改</title>
<%@include file="/commons/include/upd.jsp"%>
<link rel="stylesheet" href="${ctx}/js/jquery/ui/css/jquery-ui.css" />
<script src="${ctx}/js/jquery/ui/jquery-ui.js"></script>
<script src="${ctx}/js/jquery/ui/jquery.ui.combobox.js"></script>
<script type="text/javascript" src="${ctx}/js/calendar/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
$(function(){
	
	jQuery("#dataForm").validate(
	{
		rules: {
		},
		messages: {
		},
		submitHandler:function(form)
		{
			setGoodsInfo();
			jQuery(form).ajaxSubmit(_options);
		}
	});
	if($("#custom_code option").lenght > 0)
		$("#custom_code").combobox();
});
_callback = function() {
	location.href = ctx + "/manage/sell/getOrder.htm";
}
validCallBack = function()
{
	//添加页面自定义校验

	return true;
};
function setGoodsInfo() {
	var goodsInfoStr = "";
	//添加页面自定义校验
	for(var id in goods) {
		if(goods[id] != null)
			goodsInfoStr += id + ",";
	}
	//alert(goodsInfoStr);
	$("#goodsInfo").attr("value", goodsInfoStr);
}
var goods = {};
var action = ${action};
//JavaScript Document
function selectGoods() {
	//showModelessDialog
	//encodeURI、encodeURIComponent
	var source = encodeURIComponent(ctx+"/manage/basicData/selectGoods.htm?typeid=&classId=&r=" + Math.random());
	var url = ctx + "/commons/showDialog.jsp?r=" + Math.random() + "&url=" + source;
	var result = window.showModalDialog(url, this, "dialogWidth=600px;dialogHeight=600px;scroll=no");
	var panel = $("#MGoodsPanel");
	var html = "";
	for(var i = 0; i < result.length; i++) {
		var v = result[i];
		if(typeof(goods[v.id]) == "undefined" || goods[v.id] == null) {
			goods[v.id] = v.sn;
			html += '    <tr>'
				+ '      <td>' + v.sn + '<input type="hidden" name="wprice_' + v.id + '" id="wprice_' + v.id + '" value="' + v.wprice + '"  /></td>'
				+ '      <td>' + v.name + '</td>'
				+ '      <td>' + v.weight + '</td>'
				+ '      <td>' + v.unit + '</td>'
				+ '      <td><input name="amount_' + v.id + '" type="text" id="amount_' + v.id + '" value="0" size="10"  onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')" /></td>';
			if(action == 2) {
				html += '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>';
			} else {
				html += '      <td><input name="discount_' + v.id + '" type="text" id="discount_' + v.id + '" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')" value="100" size="10"  />% </td>'
					+ '      <td id="sprice_td_' + v.id + '"><input name="sprice_' + v.id + '" type="text" id="sprice_' + v.id + '" size="10" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')"></td>'
					+ '      <td id="totalM_' + v.id + '"><input name="sumMoney_' + v.id + '" type="text" id="sumMoney_' + v.id + '" size="10" onChange="calcGoodsTotalM(this,' + v.id + ',' + v.wprice + ')"></td>';
			}
				html += '      <td><a href="#" onClick="deleteGoods(this, ' + v.id + ')">删除</a></td>'
				+ '    </tr>';
		}
	}
	panel.append(html);
}
function deleteGoods(el, id) {
	var row = $(el).parent().parent();
	row.remove();
	goods[id] = null;
	delete goods[id];
}
function calcGoodsTotalM(el, id) {
	if(action == 2)return;
	var obj = $(el);
	var prefix = obj.attr("name").split('_')[0];
	var sprice = $("#sprice_"+id).val();
	var discount = $("#discount_"+id).val();
	var sumMoney = $("#sumMoney_"+id).val();
	var wprice = $("#wprice_"+id).val();
	var amount = $("#amount_"+id).val();
	if(prefix == "discount") {
		$("#sprice_"+id).attr("value", discount * wprice / 100);
		$("#sumMoney_"+id).attr("value",(discount * wprice / 100 * amount).toFixedU(2));
	} else if(prefix == "sprice") {
		$("#discount_"+id).attr("value", parseInt(sprice / wprice * 100));
		$("#sumMoney_"+id).attr("value", (sprice * amount).toFixedU(2));
	} else {
		$("#discount_"+id).attr("value", parseInt((sumMoney / amount).toFixedU(2) / wprice * 100));
		$("#sprice_"+id).attr("value", (sumMoney / amount).toFixedU(2));
	}
}
</script>
</head>
<body>
	<table border="0" cellspacing="0" cellpadding="0" class="listLogo">
		<tr>
			<td class="title">订单操作</td>
			<td>
				<div class="menuTool">
					<a class="save" id="dataFormSave" href="#">保存</a> <a class="back"
						href="getOrder.htm">返回</a>
				</div></td>
		</tr>
	</table>
	<div class="line"></div>
	<form id="dataForm" method="post" action="updOrder${action}.htm">
		<input type="hidden" name="goodsInfo" id="goodsInfo" value="" />
		<table border="0" cellspacing="1" cellpadding="0" class="listTable">
			<tr>
				<td class="form_title">订单号：</td>
				<td class="form_input">${fn:escapeXml(po.order_sn)}</td>
			</tr>
			<c:choose>
				<c:when test="${action == 2}">
					<tr>
						<td class="form_title">客户代码：</td>
						<td class="form_input">
                          <c:choose>            
                          <c:when test="${loginUser.userNature == 2 || userNature == 2}">	
                          ${po.custom_code}<input type="hidden" id="custom_code" name="custom_code" value="${po.custom_code}" />
                          </c:when>
                          <c:otherwise>
                          <select name="custom_code" id="custom_code">
                          <c:forEach items="${customCodes}" var="d" varStatus="status">
                          <option value="${d.custom_code}"<c:if test="${d.custom_code eq po.custom_code}"> selected</c:if>>${d.custom_name}[${d.custom_code}]</option>
                          </c:forEach>
                          </select>
                          </c:otherwise>
                          </c:choose>  
						</td>
					</tr>
					<tr>
						<td class="form_title">制单日期：</td>
						<td class="form_input"><input type="text" id="create_date"
							name="create_date"
							value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.create_date}" />"
							readonly="readonly" class="Wdate"
							onFocus="WdatePicker({isShowWeek:true})" />
						</td>
					</tr>
					<tr>
						<td class="form_title">备注：</td>
						<td class="form_input"><textarea name="remark" cols="50"
								rows="5" id="remark">${fn:escapeXml(po.remark)}</textarea>
						</td>
					</tr>
				</c:when>
				<c:when test="${action == 10}">
					<tr>
						<td class="form_title">提交日期：</td>
						<td class="form_input"><input type="text" id="submit_date"
							name="submit_date"
							value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.submit_date}" />"
							readonly="readonly" class="Wdate"
							onFocus="WdatePicker({isShowWeek:true})" />
						</td>
					</tr>
					<tr>
						<td class="form_title">备注：</td>
						<td class="form_input"><textarea name="remark" cols="50"
								rows="5" id="remark">${fn:escapeXml(po.remark)}</textarea>
						</td>
					</tr>
				</c:when>
				<c:when test="${action == 6}">
					<tr>
						<td class="form_title">递交下一级报价日期：</td>
						<td class="form_input"><input type="text" id="baojia_date"
							name="baojia_date"
							value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.baojia_date}" />"
							readonly="readonly" class="Wdate"
							onFocus="WdatePicker({isShowWeek:true})" />
						</td>
					</tr>
                    <tr>
						<td class="form_title">提交备注：</td>
						<td class="form_input">${fn:escapeXml(po.remark)}
						</td>
					</tr>
					<tr>
						<td class="form_title">报价备注：</td>
						<td class="form_input"><textarea name="bj_remark" cols="50"
								rows="5" id="bj_remark">${fn:escapeXml(po.bj_remark)}</textarea>
						</td>
					</tr>
				</c:when>
				<c:when test="${action == 4}">
					<tr>
						<td class="form_title">报价后递交下一级确认日期：</td>
						<td class="form_input"><input type="text" id="baojia_qr_date"
							name="baojia_qr_date"
							value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.baojia_qr_date}" />"
							readonly="readonly"　class="Wdate"
							onFocus="WdatePicker({isShowWeek:true})" />
						</td>
					</tr>
					<tr>
						<td class="form_title">取消订单</td>
						<td class="form_input" style="color:#F00;"><input
							name="status" type="checkbox" value="6"> 取消订单</td>
					</tr>
					<tr>
						<td class="form_title">备注：</td>
						<td class="form_input"><textarea name="qr_remark" cols="50"
								rows="5" id="qr_remark">${fn:escapeXml(po.qr_remark)}</textarea>
						</td>
					</tr>
                    <tr>
						<td class="form_title">报价备注：</td>
						<td class="form_input">${fn:escapeXml(po.bj_remark)}
						</td>
					</tr>
				</c:when>
				<c:when test="${action == 8}">
					<tr>
						<td class="form_title">用户确认订单报价后递交下一级审核日期：</td>
						<td class="form_input"><input type="text" id="baojia_sh_date"
							name="baojia_sh_date"
							value="<fmt:formatDate pattern="yyyy-MM-dd" value="${po.baojia_sh_date}" />"
							readonly="readonly" class="Wdate"
							onFocus="WdatePicker({isShowWeek:true})" />
						</td>
					</tr>
					<tr>
						<td class="form_title">审核不通过</td>
						<td class="form_input" style="color:#F00;"><input
							name="status" type="checkbox" value="7"> 审核不通过</td>
					</tr>
					<tr>
						<td class="form_title">审核备注：</td>
						<td class="form_input"><textarea name="sh_remark" cols="50"
								rows="5" id="sh_remark">${fn:escapeXml(po.sh_remark)}</textarea>
						</td>
					</tr>
                    <tr>
						<td class="form_title">提交备注：</td>
						<td class="form_input">${fn:escapeXml(po.remark)}
						</td>
					</tr>
                     <tr>
						<td class="form_title">报价备注：</td>
						<td class="form_input">${fn:escapeXml(po.bj_remark)}
						</td>
					</tr>
                     <tr>
						<td class="form_title">确认备注：</td>
						<td class="form_input">${fn:escapeXml(po.qr_remark)}
						</td>
					</tr>
				</c:when>
			</c:choose>
			<tr>
				<td class="form_title">产品报价</td>
				<td class="form_input"><table width="340" border="0"
						cellpadding="2" cellspacing="1" class="listLogo" id="MGoodsPanel">
						<tr>
							<td>产品代码</td>
							<td>产品名称</td>
                            <td>重量</td>
							<td>计量单位</td>
							<td>订购数量</td>
							<td>产品价标</td>
							<td>产品单价</td>
							<td>产品金额</td>
                            <c:if test="${action == 6 || action ==4 || action == 8 }">
							<td>库存量</td>
							<td>下订单数量</td>
                            </c:if>
							
							<c:if test="${action == 2 }">
								<td><input type="button" name="button" id="button"
									value="增加产品" onClick="selectGoods()">
								</td>
							</c:if>
						</tr>
						<c:forEach items="${refGoods}" var="rg" varStatus="status">
							<tr>
								<td><script>goods[${rg.goods_id}] = '${rg.goods_sn}';</script>${rg.goods_sn}<input
									type="hidden" name="ref_id" value="${rg.id}" /> <input
									type="hidden" name="wprice_${rg.goods_id}"
									id="wprice_${rg.goods_id}" value="${rg.wprice}" />
								</td>
								<td>${rg.goods_name}</td>
                                <td>${rg.weight}</td>
								<td>${rg.unit}</td>
								<td><c:choose>
										<c:when test="${action != 2 }">${rg.amount}<input
												type="hidden" name="amount_${rg.goods_id}"
												id="amount_${rg.goods_id}" value="${rg.amount}" />
										</c:when>
										<c:otherwise>
											<input name="amount_${rg.goods_id}" type="text"
												id="amount_${rg.goods_id}" value="${rg.amount}" size="10" />
										</c:otherwise>
									</c:choose></td>
								<td><c:choose>
										<c:when test="${action != 6 }"><c:if test="${action != 2 }">${rg.discount}</c:if><input
												type="hidden" name="discount_${rg.goods_id}"
												id="discount_${rg.goods_id}" value="${rg.discount}" />
										</c:when>
										<c:otherwise>
											<input name="discount_${rg.goods_id}" type="text"
												id="discount_${rg.goods_id}"
												onchange="calcGoodsTotalM(this,${rg.goods_id})"
												value="${rg.discount}" size="10" />
										</c:otherwise>
									</c:choose>%</td>
								<td id="sprice_td_${rg.goods_id}"><c:choose>
										<c:when test="${action != 6 }">
	      	<c:if test="${action != 2 }">${rg.sprice}</c:if><input name="sprice_${rg.goods_id}" type="hidden"
												id="sprice_${rg.goods_id}" size="10" value="${rg.sprice}">
										</c:when>
										<c:otherwise>
											<input name="sprice_${rg.goods_id}" type="text"
												id="sprice_${rg.goods_id}" size="10" value="${rg.sprice}" onChange="calcGoodsTotalM(this,${rg.goods_id})">
										</c:otherwise>
									</c:choose>
								</td>
								<td id="sumMoney_td_${rg.goods_id}"><c:choose>
										<c:when test="${action != 6 }"><c:if test="${action != 2 }">${rg.sumMoney}</c:if><input
												name="sumMoney_${rg.goods_id}" type="hidden"
												id="sumMoney_${rg.goods_id}" size="10"
												value="${rg.sumMoney}">
										</c:when>
										<c:otherwise>
											<input name="sumMoney_${rg.goods_id}" type="text"
												id="sumMoney_${rg.goods_id}" size="10"
												value="${rg.sumMoney}" onChange="calcGoodsTotalM(this,${rg.goods_id})">
										</c:otherwise>
									</c:choose>
								</td>
                                <c:if test="${action == 6 || action == 4 || action == 8 }">
								<td>${rg.stock}</td>
								<td>${rg.down_order}</td>
                                </c:if>
								<c:if test="${action == 2 }">
									<td><a href="#"
										onClick="deleteGoods(this, ${rg.goods_id})">删除</a></td>
								</c:if>
							</tr>
						</c:forEach>
					</table>
				</td>
			</tr>
		</table>
		<input type="hidden" name="id" value="${po.id}" />
	</form>
</body>
</html>
